import { useMount } from "ahooks"
import { message } from "antd"
import moment from "moment"
import qs from "qs"
export interface MenuItem {
    path: string
    name: string
    icon: string
    father?: string
    children?: Array<string>
    show: boolean
}

const MenuList = [
    { path: '/dashboard', name: '首页', icon: '', show: true },
    { path: '/intent', name: '进件服务', icon: '', children: ['/intentionalList'], show: false },
    { path: '/intentionalList', name: '待进件列表', icon: '', father: '/intent', show: false },
]

export const getMenuList = (permissionsList: Array<string>): Array<MenuItem> => {
    let menuList: Array<MenuItem> = [...MenuList]
    menuList.forEach((item, index) => {
        if (permissionsList.includes(item.path)) {
            menuList[index].show = true
        }
    })
    return menuList
}

export const getHoursTips = (): string => {
    const date = new Date()
    if (date.getHours() >= 0 && date.getHours() < 12) {
        return "上午好"
    } else if (date.getHours() >= 12 && date.getHours() < 18) {
        return "下午好"
    } else {
        return "晚上好"
    }
}

export const emptyToZero = (numStr: string | undefined): number => {
    let result = 0
    if (numStr && !isNaN(Number(numStr))) result = Number(numStr)
    return result
}

export const useMountAsync = (fn: () => void) => {
    return useMount(() => {
        setTimeout(() => {
            fn()
        }, 0);
    })
}

export const getEnumLabel = (enumList: Array<{ value: string, label: string }>, value: string): string => {
    let result = ''
    enumList.forEach(item => {
        if (item.value === value) {
            result = item.label
        }
    })
    return result
}

export const getListByKey = <T extends object, K extends keyof T>(enumList: Array<T>, key: K): Array<any> => {
    let list: Array<any> = []
    enumList.forEach(item => {
        if (key) list.push(item[key])
    })
    return list
}

export const cutArray = (array: Array<any>, subLength: number): Array<Array<any>> => {
    let index = 0;
    let newArr = [];
    while (index < array.length) {
        newArr.push(array.slice(index, index += subLength));
    }
    return newArr;
}

export const stopAndTips = (judge: boolean, msg: string, type?: 'warning' | 'error'): boolean => {
    if (judge) {
        message[type || 'warning'](msg)
        return true
    } else {
        return false
    }
}

export const getPathWithParams = (path: string, params: any): string => {
    let result = ''
    if (path) result = `${path}?${qs.stringify(params)}`
    return result
}

export const getUrlParams = (search: string): any => {
    const result = {}
    const arr_1 = search.split('?')
    const arr_2 = arr_1[1].split('&')
    arr_2.forEach(item => {
        const obj = item.split('=')
        const param = { [`${obj[0]}`]: obj[1] }
        Object.assign(result, param)
    })
    return result
}

export const formatDateValue = (value: any, format: any = 'YYYY/MM/DD'): any => {
    return value ? moment(value, format) : null
}